﻿using System;
using System.Collections.Generic;
using System.Linq;  
using System.Text;
using System.Reflection;

namespace Clandestine.Items
{
    public static class ItemDatabase
    {
        private static Dictionary<string, Type> items = new Dictionary<string, Type>();

        static ItemDatabase()
        {
            Log.i("Starting to load items...");
            DateTime start = DateTime.Now;

            // Add all classes that inherit Item
            // Note: Do not make effects in other assemblies without updating this func!
            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            for (int i = 0; i < types.Length; i++)
                if (types[i].IsSubclassOf(Assembly.GetExecutingAssembly().GetType("Clandestine.Items.Item")) && !types[i].IsAbstract)
                    items.Add(((Item)Activator.CreateInstance(types[i])).Name, types[i]);

            Log.i("Finished loading items... Took {0} ticks.", DateTime.Now.Ticks - start.Ticks);
        }

        /// <summary>
        /// Creates an instance of an item.
        /// </summary>
        /// <param name="name">The name of the item to create.</param>
        /// <returns>The instance of the named item created.</returns>
        public static Item CreateItem(string name)
        {
            if (items.ContainsKey(name))
                return (Item)Activator.CreateInstance(items[name]);
            else
                throw new ArgumentException("Could not find an item with the specified name.");
        }
    }
}

